<template>
	<view>
		<view class="item" v-for="(res, index) in siteList" :key="res.id">
			<view @click="changeAddr(index)">
				<view class="top">
					<view class="name">{{ res.consignee }}</view>
					<view class="phone">{{ res.phone }}</view>
					<view class="tag">
						<text v-if="res.status=='0'" class="red">默认</text>
						<text v-if="res.label!=''">{{res.label}}</text>
					</view>
				</view>
				<view class="bottom">
					{{res.province}} {{res.city}} {{res.district}} {{res.address}}
					<view @click.stop="toUpdateAddr(res)"><u-icon  name="edit-pen" :size="40" color="#999999"></u-icon></view>
				</view>
			</view>
		</view>
		<view class="addSite" @tap="toAddSite">
			<view class="add">
				<u-icon name="plus" color="#ffffff" class="icon" :size="30"></u-icon>新建收货地址
			</view>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			siteList: [],
			orderId:[]
		};
	},
	onLoad(data) {
		if(data.orderId!==""&&data.orderId!==undefined){
			this.orderId=data.orderId.split("_")
		}
	
	},
	onShow() {
		this.$u.api.userAddress().then(res=>{
			this.siteList=res
		})
	},
	methods: {
		changeAddr(index){
			if(this.orderId.length!==0){
				
				let g=this.orderId.length
				let t=0
				for(let i=0;i<this.orderId.length;i++){
					this.siteList[index].orderId=this.orderId[i]
					this.$u.api.updateOrder(this.siteList[index]).then(()=>{
						t=t+1
						if(t==g){
							uni.navigateBack()
						}
					})
				}
				this.orderId.forEach(t=>{
					
				})
				
			}else{
				let pages = getCurrentPages();
				let prePage = pages[pages.length - 2];
				if(prePage.$vm.orderData!==undefined){
					prePage.$vm.orderData.selectedAddr=this.siteList[index]
					uni.navigateBack()
				}
				
			}
			
			
		},
		toUpdateAddr(v){
			this.$u.route({
				url: 'pages/address/addSite',
				params: v
			})
		},
		toAddSite(){
			uni.navigateTo({
			    url: '/pages/address/addSite'
			});
		}
	}
};
</script>

<style lang="scss" scoped>
.item {
	padding: 40rpx 20rpx;
	.top {
		display: flex;
		font-weight: bold;
		font-size: 34rpx;
		.phone {
			margin-left: 60rpx;
		}
		.tag {
			display: flex;
			font-weight: normal;
			align-items: center;
			text {
				display: block;
				width: 60rpx;
				height: 34rpx;
				line-height: 34rpx;
				color: #ffffff;
				font-size: 20rpx;
				border-radius: 6rpx;
				text-align: center;
				margin-left: 30rpx;
				background-color:rgb(49, 145, 253);
			}
			.red{
				background-color:red
			}
		}
	}
	.bottom {
		display: flex;
		margin-top: 20rpx;
		font-size: 28rpx;
		justify-content: space-between;
		color: #999999;
	}
}
.addSite {
	display: flex;
	justify-content: space-around;
	width: 600rpx;
	line-height: 100rpx;
	position: absolute;
	bottom: 30rpx;
	left: 80rpx;
	background-color: red;
	border-radius: 60rpx;
	font-size: 30rpx;
	.add{
		display: flex;
		align-items: center;
		color: #ffffff;
		.icon{
			margin-right: 10rpx;
		}
	}
}
</style>
